package com.ruoyi.workflow.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.workflow.mapper.StoreInfoMapper;
import com.ruoyi.workflow.domain.StoreInfo;
import com.ruoyi.workflow.service.IStoreInfoService;

/**
 * 门店管理Service业务层处理
 *
 * @author yeqi
 * @date 2022-11-24
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements IStoreInfoService {

    @Autowired
    private ISysUserService userService;
    /**
     * 查询门店管理列表
     *
     * @param storeInfo 门店管理
     * @return 门店管理
     */
    @Override
    public List<StoreInfo> selectStoreInfoList(StoreInfo storeInfo) {
        return getBaseMapper().selectStoreInfoList(storeInfo);
    }

    // 查询列表
    @Override
    public List<StoreInfo> selectStoreInfoListSome(StoreInfo storeInfo) {

        return getBaseMapper().selectStoreInfoListSome( storeInfo);
    }

    @Override
    public boolean updateByStatus(StoreInfo storeInfo) {
        Long userId = SecurityUtils.getUserId();

        return  update(new LambdaUpdateWrapper<StoreInfo>()
                .set(StoreInfo::getStatus,2)
                .set(StoreInfo::getUpdatorId,userId)
                .set(StoreInfo::getUpdatedTime,new Date())
                .eq(StoreInfo::getId,storeInfo.getId())
        );
    }

    @Override
    public boolean save(StoreInfo entity) {
        if (entity.getStatus() == 2){
            throw new ServiceException("创建时状态不能是已删除");
        }
        // 基于 管理员ID 查询 管理原信息
        SysUser user = userService.selectUserById(entity.getManagerId());
        // 设值进去
        entity.setManagerName(user.getNickName());
        entity.setManagerTel(user.getPhonenumber());
        // 查询当前登录用户 添加创建人ID
        Long userId = SecurityUtils.getUserId();
        entity.setCreatorId(userId);
        // 添加创建时间
        entity.setCreatedTime(new Date());
        return super.save(entity);
    }

    @Override
    public boolean updateById(StoreInfo entity) {
        if (entity.getStatus() == 2){
            throw new ServiceException("创建时状态不能是已删除");
        }
        // 添加更新时间和更新人Id
        Long userId = SecurityUtils.getUserId();
        entity.setUpdatorId(userId);
        entity.setUpdatedTime(new Date());

        return super.updateById(entity);
    }


}
